<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Fans; 
use app\model\Scan; 

class Friendhelp extends Base
{
    public function Friendhelp()
    {
global $_G, $_P;
$system    = $_G['system'];
$fans = $this->checkuser();
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$themeid = intval($_P['themeid']);
// $theme = Theme::getThemeById($themeid);
$config = Theme::getConfig($themeid);
$fuid = $_G['member']['uid'];
$uid = $_P['uid'];
$result = array();

if(empty($uid)){
	$result = array(
		"errcode"=>2,
		"msg"=>'参数错误',
	);
	return_msg($result, '', 'ajax');
}
if($uid == $fuid){
	$result = array(
		"errcode"=>2,
		"msg"=>'不可给自己'.$config['friend_name'],
	);
	return_msg($result, '', 'ajax');
}


if($config['friendorder']==0){
    //如果已定单,客服为定单客服
    $p3 = array(":uid"=>$uid,":themeid"=>$themeid);
    if ($config['joinfee']=='0.00' && $config['groupprice']=='0.00'){
        $friendorder = db_find ( "SELECT * FROM " . tablename (t_record) . "
                                    WHERE themeid =:themeid
                                            and uid = :uid and type<>8 order by id desc limit 1",$p3);
    }else{
        $friendorder = db_find ( "SELECT * FROM " . tablename (t_record) . "
                                    WHERE themeid =:themeid
                                            and uid = :uid and (status > 0 or status=-2)  order by id desc limit 1",$p3);
    }
    if(empty($friendorder)){
        $result = array(
            "errcode"=>2,
            "msg"=>'该用户还没有报名',
        );
        return_msg($result, '', 'ajax');
    }
}


if($system['friend'] !=1 && $config['isicon'] !=3){
	$result = array(
		"errcode"=>2,
		"msg"=>'功能未开启',
	);
	return_msg($result, '', 'ajax');
}

    $params = array();
    
    $params[':uid'] = $fuid;
    $params[':fromuid'] = $uid;
    $params[':themeid'] = $themeid;
    $friend = db_find("SELECT * FROM ".tablename(t_friend)."
		WHERE uid = :uid and themeid = :themeid and fromuid = :fromuid",$params);

	if(empty($friend)){
        $data = array(
            'themeid'   => $themeid,
            'uniacid'   => $_G['mpid'],
            'uid'       => $_G['member']['uid'],
            'openid'    => $_G['openid'],
            'nickname'  => $fans['nickname'],
            'avatar'    => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
            'status'    => 0,
            'addtime'   => TIMESTAMP,
            'fromuid'  => $uid,
            'fopenid'  => $fopenid,
            'workerid' => $workerid,
        );
        
	    if ($system['hdget']==1 &&Fans::getuid_old($workerid,$themeid)) {
	        $data['workerid_old']  = $workerid;
	    }

	    if ($system['hdget']==1 &&Fans::getuid_old($workerid,$themeid)) {
	        $data['workerid_old']  = $workerid;
	    }
        // if($config['iconlimit'] > 0){
        //     $todaynum = Scan::getFpenidTodayNum($themeid,$fopenid);
        //     if($todaynum < $config['iconlimit']){
        //         $data['status'] = 1;
        //     }
        // }else{
            $data['status'] = 1;
        // }
        $insertid = db_insert(t_friend, $data);

		$pm = array(":themeid"=>$themeid,":uid"=>$uid);
		
		db_query("UPDATE ".tablename(t_fans)." SET friendnum = friendnum +1 WHERE themeid =:themeid and uid = :uid",$pm);

		$result = array(
			"errcode"=>1,
			"msg"=>$config['friend_name'].'成功',
		);
		return_msg($result, '', 'ajax');
    }else{
		$result = array(
			"errcode"=>2,
			"msg"=>$config['friend_name'].'失败',
		);
		return_msg($result, '', 'ajax');
    }
}
}